package net.itgoo.parkingpay.ui.rfid;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;

import net.itgoo.parkingpay.rest.ParkingVolleyManager;
import net.itgoo.parkingpay.vendor.mvp.ParkingMVPLoadDataCallback;

import org.json.JSONException;
import org.json.JSONObject;

public class ParkingRfidDataRepository implements ParkingRfidContract.DataSource {

    @Override
    public void getData(ParkingMVPLoadDataCallback callback) {

    }

    @Override
    public void bind(String userId, String plate, String phone, String tag, String code, final ParkingMVPLoadDataCallback callback) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("plateno", plate);
            jsonObject.put("appuserid", userId);
            jsonObject.put("phone", phone);
            jsonObject.put("tagno", tag);
            jsonObject.put("otpcode", code);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        ParkingVolleyManager.sendJsonObjectRequest(Request.Method.POST,
                ParkingVolleyManager.baseUrl + "App_bandRfidCard.do", jsonObject,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        int code = 0;
                        String msg = "";
                        String ccid = "";
                        try {
                            code = response.getInt("code");
                            if (response.has("ccid")) {
                                ccid = response.getString("ccid");
                            }
                            if (response.has("msg")) {
                                msg = response.getString("msg");
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                        if (code == 200) {
                            callback.onDataLoaded(ccid);
                        } else {
                            callback.onDataError(msg);
                        }
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        callback.onDataError(error.getMessage());
                    }
                });
    }

    @Override
    public void auto(String cardId, int isAuto, final ParkingMVPLoadDataCallback callback) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("ccid", Long.valueOf(cardId));
            jsonObject.put("isauto", isAuto);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        ParkingVolleyManager.sendJsonObjectRequest(Request.Method.POST,
                ParkingVolleyManager.baseUrl + "App_updateRfidAutoState.do", jsonObject,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        int code = 0;
                        String msg = "";
                        try {
                            code = response.getInt("code");
                            if (response.has("msg")) {
                                msg = response.getString("msg");
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                        if (code == 200) {
                            callback.onDataLoaded("");
                        } else {
                            callback.onDataError(msg);
                        }
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        callback.onDataError(error.getMessage());
                    }
                });
    }

    @Override
    public void cancel() {

    }
}
